<?php

namespace V1;
class ArticleList extends \ResourceClass
{
    public $isActive = true;
    public $remark = '文章列表';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'id require';
        }
        try {
            $result = array();
            $uids = array();
            $ids = array();
            $where = '1';
            if (isset($req['status'])) {
                $where .= " AND `status` = {$req['status']}";
            }
            $fabulous = new \Biz\Fabulous();
            $article_list = new \Orm\Article\ArticleList();
            $article_list['key'] = isset($req['l']) ? $req['l'] : \Util\Config::get('default_limit');
            $article_list['l'] = isset($req['l']) ? $req['l'] : \Util\Config::get('default_limit');
            $article_list['o'] = isset($req['o']) ? $req['o'] : \Util\Config::get('default_offset');
            $article_list['where'] = $where;
            $article_list['key_by'] = 'id';
            $order = 'insert_time desc';
            $article_list['order'] = $order;
            $article_list->load();
            foreach ($article_list['lists'] as $id => $list){
//                $res = $fabulous ->completeFabulous(346,101);
                $res = $fabulous ->completeFabulous((string)$list['id'],$req['uid']);
                $result[$id]['id'] = (string)$list['id'];
                $result[$id]['uid'] = (string)$list['uid'];
                $result[$id]['title'] = (string)$list['title'];
                $result[$id]['img'] = (string)$list['img'];
                $result[$id]['fabulous_sum'] = $res['sum_fabulous'];
                $result[$id]['fabulous_id'] = $res['fabulous_id'];
                $result[$id]['reading_times'] = (string)$list['reading_times'];
                $result[$id]['remark'] = (string)$list['remark'];
                $result[$id]['insert_time'] = (string)$list['insert_time'];
                $result[$id]['update_time'] = (string)$list['update_time'];
                $result[$id]['user_info'] = array();
                $result[$id]['content_list'] = array();
                array_push($ids,$list['id']);
                array_push($uids,$list['uid']);
            }
            $user_list = $article_list->completeUserInfo($uids);
            if(count($user_list)){
                foreach($article_list['lists'] as $id => $list) {
                    foreach ($user_list as $k => $value) {
                        if ($list['uid'] == $value['id']) {
                            $result[$id]['user_info'] = array(
                                'id' => $value['id'],
                                'open_id' => $value['open_id'],
                                'nickname' => $value['nickname'],
                                'headimgurl' => $value['headimgurl'],
                            );
                        }
                    }
                }
            }
            $content_list = $article_list->completeContent($ids);
            if(count($content_list)){
                foreach ($content_list as $item) {
                    $result[$item['article_id']]['content_list'][] = array(
                        'id' => $item['id'],
                        'article_id' => $item['article_id'],
                        'type' => $item['type'],
                        'content' => $item['content'] ,
                        'sort' => $item['sort'] ,
                    );
                }
            }
        } catch (\Base\Exception\MysqlErrorException $e) {
            return '服务器内部错误'.$e->getMessage();
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}